package com.df.datacloud.filter;
import com.df.datacloud.utils.RSAUtils;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
@Component
public class AccessFilter extends ZuulFilter  {
    private static Logger log = LoggerFactory.getLogger(AccessFilter.class);
    
    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }
    
    @Override
    public int filterOrder() {
        return 0;
    }
    
    @Override
    public boolean shouldFilter() {
        //final String serviceId = (String) RequestContext.getCurrentContext().get("proxy");
        return  true;
    }
    
    @Override
    public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();
        ctx.setSendZuulResponse(true);
        ctx.setResponseStatusCode(200);
        log.info(" credit access token is right22222");
        return null;
    }
    
    
    
    private Object test(){
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest  req = ctx.getRequest();
        String requestUrl = req.getRequestURI();
        Map<String, String> map = getHeadersInfo(req);
        String mobile = map.get("x-user-mobile");
        String timestamp =  map.get("x-timestamp");
        String partner = map.get("x-partner-name");
        String signData = map.get("x-sign-data");
        /**
         *  放行接口
         */
        if(!StringUtils.isEmpty(requestUrl) && requestUrl.contains("pub")){
            ctx.setSendZuulResponse(true);
            ctx.setResponseStatusCode(200);
            log.info(" credit access token is right ");
            return null;
        }
    
        Boolean flag = false;
        if(null!=mobile && null!=timestamp&&null!=partner&&null!=signData) {
            StringBuilder sb= new StringBuilder(partner).append("&").append(mobile).append("&").append(timestamp);
            if(RSAUtils.verification(sb.toString(), signData)) {
                flag =true;
            }
        }
    
        if(!flag){
            ctx.setSendZuulResponse(true);
            ctx.setResponseStatusCode(200);
            log.info(" credit access token is right ");
            return null;
        }else{
            log.error("权限不足导致访问错误，非法用户");
            ctx.setSendZuulResponse(false);
            ctx.setResponseStatusCode(401);
            ctx.setResponseBody("访问失败，请确认合法性再访问!");
            return null;
        }
    }
    
    private Map<String, String> getHeadersInfo(HttpServletRequest request) {
        Map<String, String> map = new HashMap<String, String>();
        Enumeration headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String key = (String) headerNames.nextElement();
            String value = request.getHeader(key);
            map.put(key, value);
        }
        return map;
    }
}
